package com.example.demo.service.impl;

import com.example.demo.entity.Result;
import com.example.demo.mapper.InterfaceMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class InterfaceService {
    @Autowired
    private InterfaceMapper interfaceMapper;

    public Result getAllInterface(Map<String, Object> params) {
       List<Map<String,Object>> list= interfaceMapper.getAllInterface(params);
       return Result.ok(list);
    }

    @Transactional
    public Result insertInterface(Map<String, Object> params) {
        interfaceMapper.insertInterface(params);
        return Result.ok("新增接口成功");
    }
    @Transactional
    public Result deleteInterface(Map<String, Object> params) {
        interfaceMapper.deleteInterface(params);
        return Result.ok("删除接口成功");
    }
    @Transactional
    public Result insertRoleInterface(Map<String, Object> params) {
        List<Map<String, Object>> allInterface = interfaceMapper.getAllInterface(params);
        if(allInterface.size()>0){
            allInterface.stream()
                    .forEach(it->{
                        Map<String,Object> map=new HashMap<>();
                        map.put("roleId",params.get("roleId"));
                        map.put("interId",it.get("interId"));
                        interfaceMapper.deleteRoleInterface(map);
                    });
        }
        if(Objects.nonNull(params.get("interId"))){
            List<String> list= (List<String>) params.get("interId");
            if(list.size()>0){
                interfaceMapper.insertRoleInterface(params);
            }
        }
        return Result.ok("配置接口成功");
    }

    public Result getRoleInterface(Map<String, Object> params) {
        List<Map<String,Object>> list= interfaceMapper.getRoleInterface(params);
        return Result.ok(list);
    }
}
